//
//  FirstTwoCell.m
//  ShopDemo
//
//  Created by 飞扬的青春 on 2018/7/9.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "FirstTwoCell.h"
#import "FirstTwoCollViewCell.h"
#import "FirDetailController.h"
@implementation FirstTwoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UILabel *leftlabe = [UILabel new];
        leftlabe.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
        leftlabe.textColor = [UIColor redColor];
        leftlabe.textAlignment = NSTextAlignmentLeft;
        self.leftlab = leftlabe;
        [self.contentView addSubview:leftlabe];
        leftlabe.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 8*kJLXWidthScale)
        .widthIs(kScreenWidth/2)
        .heightIs(40*kJLXWidthScale);
        
        UILabel *rightlab = [UILabel new];
        rightlab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
        rightlab.text = @"查看更多>";
        rightlab.textColor = [UIColor grayColor];
        rightlab.textAlignment = NSTextAlignmentRight;
        self.rightlab = rightlab;
        [self.contentView addSubview:rightlab];
        rightlab.sd_layout
        .topSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 8*kJLXWidthScale)
        .widthIs(kScreenWidth/2)
        .heightIs(40*kJLXWidthScale);
        
       self.imageVi = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40*kJLXWidthScale, kScreenWidth, 180*kJLXWidthScale)];
        [self.contentView addSubview:self.imageVi];
        [self creatCollectionviewMethod];
        
    }
    return self;
}

-(void)creatCollectionviewMethod{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.listCollectionview = [[ UICollectionView alloc ] initWithFrame:CGRectMake(0, 300*kJLXWidthScale, kScreenWidth, kScreenWidth/3*2 + 20*kJLXWidthScale) collectionViewLayout :layout];
    [self.contentView addSubview:self.listCollectionview];
    self.listCollectionview.sd_layout
    .topSpaceToView(self.imageVi, 0)
    .bottomSpaceToView(self, 10*kJLXWidthScale);
    if (kScreenHeight == 812) {
        self.listCollectionview.sd_layout
        .bottomSpaceToView(self, 34);
    }
    self.listCollectionview.delegate = self;
    self.listCollectionview.dataSource = self;
    self.listCollectionview.backgroundColor = [UIColor whiteColor];
    [self.listCollectionview registerClass :[ FirstTwoCollViewCell class ] forCellWithReuseIdentifier : @"twocell" ];
}


#pragma mark--------------------UICollectionViewDelegateFlowLayout,UICollectionViewDelegate, UICollectionViewDataSource
//cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
//
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FirstTwoCollViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"twocell" forIndexPath:indexPath];
    NSDictionary *dic = self.dataArray[indexPath.row];
    NSURL *url = [NSURL URLWithString:[MethodClass returnUrlSetStrWSDLURL:dic[@"CATEGORY_IMG"]]];
    cell.titImageview.center = cell.contentView.center;
    [cell.titImageview sd_setImageWithURL:url];
    cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"CATEGORY_NAME"]];
    return cell;
    
}
#pragma mark-------------------布局代理
//设置每个cell的宽和高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth / 3 -1, kScreenWidth /3);
}
//设置分区缩进
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(8*kJLXWidthScale, 0, 8*kJLXWidthScale, 0);
}
//设置每个分区之间最小的行间距和列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
#pragma mark--------------------------cell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.detailBlock(self.dataArray[indexPath.row]);
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
